<?php
class tags_labelModel extends baseModel{

    public function tags_list($data){

        if(!empty($data['cid'])){
            $where['cid'] = ' AND B.cid in(' . $data['cid'] . ')';
            $table_class=" inner join {$this->model->pre}tags_category B ON A.cid=B.cid  ";
            $table_class_field=",B.name as cname";
        }
        if(!empty($data['aid'])){
            $where['aid'] = ' AND C.aid in(' . $data['aid'] . ')';
            $table_relation=" inner join {$this->model->pre}tags_relation C ON A.id=C.tid  ";
            $table_relation_field=",C.aid";
        }
        if (!empty($data['where'])) {
            $where['where'] = $data['where'];
        }
        if (!empty($data['limit'])) {
            $where['limit'] = intval($data['limit']);
        }else{
            $where['limit'] = 10;
        }
        if (!empty($data['order'])) {
            $where['order'] = $data['order'];
        }else{
            $where['order'] = 'id desc';
        }
        if (!empty($data['rand'])) {
            $where['order'] = 'rand()';
        }
        $condition=$where['aid'].$where['cid'].$where['where'];
        if(!empty($condition)){
            $condition='WHERE '.$condition;
        }
        $sql="
            SELECT  distinct  A.*{$table_class_field}{$table_relation_field}
            FROM {$this->model->pre}tags A 
            {$table_class}
            {$$table_relation}
            {$condition}
            ORDER BY {$where['order']} 
            LIMIT {$where['limit']} 
            ";
        $data=$this->query($sql);
        if(!empty($data)){
            $i=0;
            foreach ($data as $key=>$value) {
                $i++;
                $list[$key]=$value;
                $list[$key]['aurl']=url('tags/index/info',array('name'=>$value['name']));
                $list[$key]['i']=$i;
            }
        }
        return $list;

    }


}

?>